﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
using MediaPortal.GUI.Library;


using FlickrNet;

using OpenSocialPlugin.General;
using OpenSocialPlugin.Providers.Sites.Flickr;

namespace OpenSocialPlugin.Providers
{
    class Flickr : BaseSite, ISocialSite
    {
        SiteSettings _settings = new SiteSettings();
        FlickrNet.Flickr service = new FlickrNet.Flickr("386a49f340dd0f2201b2c4ca26271b31");
        public Flickr()
        {
            _settings.Load();
            _capabilities = new List<CapabilitiesEnum>();
            settingForm = new SettingControl();
            settingForm.settings = _settings;
            _capabilities.Add(CapabilitiesEnum.CanSearch);
            _capabilities.Add(CapabilitiesEnum.HasFriend);
            _capabilities.Add(CapabilitiesEnum.HasPhotos);
            _capabilities.Add(CapabilitiesEnum.HasOtherFriend);
            _capabilities.Add(CapabilitiesEnum.HasOtherPhotos);
            _capabilities.Add(CapabilitiesEnum.HasExtras);
        }


        #region ISocialSite Members

        public List<OSP_Person> GetFriends(OSP_Person person)
        {
            List<OSP_Person> resp = new List<OSP_Person>();
            ContactCollection contacts = service.ContactsGetPublicList(person.Id);
            foreach (Contact cont in contacts)
            {
                resp.Add(GetPerson(cont.UserId));
            }
            return resp;
        }

        public List<OSP_Album> GetAlbums(OSP_Person person)
        {
            List<OSP_Album> resp = new List<OSP_Album>();
            OSP_Album album1 = new OSP_Album("0", person.Id);
            album1.Title = "All photos";
            resp.Add(album1);
            PhotosetCollection sets = service.PhotosetsGetList(person.Id);
            foreach (Photoset set in sets)
            {
                OSP_Album album = new OSP_Album(set.PhotosetId, set.OwnerId);
                album.Thumb = set.PhotosetThumbnailUrl;
                album.NumPhotos = set.NumberOfPhotos;
                album.Title = set.Title;
                resp.Add(album);
            }
            return resp;
        }

        private List<OSP_Photo> ToPhotolist(PhotoCollection usersPhotos)
        {
            List<OSP_Photo> resp = new List<OSP_Photo>();
            foreach (Photo photo in usersPhotos)
            {
                OSP_Photo ph = new OSP_Photo();
                ph.Id = photo.PhotoId;
                ph.OwnerId = photo.UserId;
                ph.AlbumId = "0";
                ph.Title = photo.Title;
                ph.Thumb = photo.ThumbnailUrl;
                try
                {
                    if (photo.DoesLargeExist)
                        ph.Url = photo.LargeUrl;
                    else if (photo.DoesMediumExist)
                        ph.Url = photo.MediumUrl;
                    else
                        ph.Url = photo.SmallUrl;
                }
                catch
                {
                    ph.Url = photo.MediumUrl;
                }
                resp.Add(ph);
            }
            return resp;
        }

        public List<OSP_Photo> GetPhotos(OSP_Album album)
        {
            List<OSP_Photo> resp = new List<OSP_Photo>();
            if (album.Id == "0")
            {
              PhotoSearchOptions userSearch = new PhotoSearchOptions();
              userSearch.UserId = album.OwnerId;
              userSearch.SortOrder = FlickrNet.PhotoSearchSortOrder.InterestingnessAscending;
              userSearch.Extras = PhotoSearchExtras.All;
              return ToPhotolist(service.PhotosSearch(userSearch));
            }
            else
            {
              PhotosetPhotoCollection set = service.PhotosetsGetPhotos(album.Id, PhotoSearchExtras.All);
              foreach (Photo photo in set)
              {
                OSP_Photo ph = new OSP_Photo();
                ph.Id = photo.PhotoId;
                ph.OwnerId = photo.UserId;
                ph.AlbumId = album.Id;
                ph.Title = photo.Title;
                ph.Thumb = photo.ThumbnailUrl;
                if (photo.DoesLargeExist)
                  ph.Url = photo.LargeUrl;
                else if (photo.DoesMediumExist)
                  ph.Url = photo.MediumUrl;
                else
                  ph.Url = photo.SmallUrl;
                resp.Add(ph);
              }
            }
            return resp;
        }

        public GeneralResponse Home()
        {
            //GeneralResponse res = new GeneralResponse();
            //OSP_Person pers = Login();
            //List<OSP_Album> list = GetAlbumss(pers);
            //res.Response = list;
            //res.Title = string.Format("Sets of {0}", pers);
            return Extras("", 0);
        }

        public GeneralResponse Search(string serchtxt, int type)
        {
            GeneralResponse res = new GeneralResponse();
            switch (type)
            {
              case 0:
                {
                  PhotoSearchOptions userSearch = new PhotoSearchOptions();
                  userSearch.Text = serchtxt;
                  userSearch.PerPage = 500;
                  userSearch.Extras = PhotoSearchExtras.All;
                  userSearch.SortOrder = FlickrNet.PhotoSearchSortOrder.Relevance;
                  res.Response = ToPhotolist(service.PhotosSearch(userSearch));
                  res.Title = string.Format("Search result for {0}", serchtxt);
                }
                break;
              case 1:
                {
                  PhotoSearchOptions userSearch = new PhotoSearchOptions();
                  userSearch.Tags = serchtxt;
                  userSearch.PerPage = 500;
                  userSearch.Extras = PhotoSearchExtras.All;
                  userSearch.SortOrder = FlickrNet.PhotoSearchSortOrder.Relevance;
                  res.Response = ToPhotolist(service.PhotosSearch(userSearch));
                  res.Title = string.Format("Search result for tag(s) {0}", serchtxt);

                }
                break;
                default:
                    break;
            }
            return res;
        }

        public GeneralResponse Extras(string serchtxt, int type)
        {
          GeneralResponse resp = new GeneralResponse();
          switch (type)
          {
            case 0:
              {
                resp.Response = ToPhotolist(service.InterestingnessGetList(PhotoSearchExtras.All, 0, 500));
                resp.Title = "Most intersting photos";
              }
              break;
            case 1:
              {
                resp.Response = ToPhotolist(service.PhotosGetRecent(PhotoSearchExtras.All));
                resp.Title = "Latest public photos";
              }
              break;
            default:
              break;
          }
          return resp;
        }

      public void Init()
        {
        }

        public OSP_Person Login()
        {
          if (string.IsNullOrEmpty(_settings.User))
            return null;
            //try
            //{
            FoundUser user = service.PeopleFindByEmail(_settings.User);
                return GetPerson(user.UserId);
            //}
            //catch
            //{
            //    return null;
            //}
        }

        private SettingControl settingForm;
        public Control SettingForm
        {
            get
            {
                return settingForm;
            }
        }

        List<CapabilitiesEnum> _capabilities;
        public List<CapabilitiesEnum> Capabilities { get { return _capabilities; } }

        public override string ToString()
        {
            return "Flickr";
        }

        public OSP_Person GetPerson(string personId)
        {
            OSP_Person pers = new OSP_Person();
            Person resp = service.PeopleGetInfo(personId);
            pers.Id = resp.UserId;
            pers.Name = resp.UserName;
            pers.Thumb = resp.BuddyIconUrl.ToString();
            return pers;

        }

      public string GetPersonInfo(OSP_Person person)
      {
        throw new NotImplementedException();
      }

      public string GetPhotoInfo(OSP_Photo photo)
      {
        throw new NotImplementedException();
      }

      public List<string> SearchNames
        {

            get
            {
                List<string> list = new List<string>();
                list.Add("Search by photo name");
                list.Add("Search by photo tag");
                return list;
            }
        }

        public List<string> ExtrasNames
        {
            get
            {
                List<string> resp = new List<string>();
                resp.Add("Most intersting photos");
                resp.Add("Latest public photos");
                return resp;
            }
        }

        #endregion
    }
}

